/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-09-08
 * Time: 19:16
 */
public class Demo6 {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            //规定加锁顺序由小到大
            synchronized (locker1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2) {
                    System.out.println("t1 线程成功获取到锁");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            //规定加锁顺序由小到大
            synchronized (locker1) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2) {
                    System.out.println("t2 线程成功获取到锁");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
